Skip to content

TYP: Simple type fixes for ExtensionArray #41255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 6, 2021

Conversation

Dr-Irv
Copy link
Contributor

@Dr-Irv Dr-Irv commented May 1, 2021

Typing changes for the following methods in ExtensionArray

  • __iter__
  • __contains__
  • fillna
  • transpose
  • ravel
  • __hash__

@Dr-Irv Dr-Irv requested a review from simonjayhawkins May 1, 2021 23:16
@simonjayhawkins simonjayhawkins added ExtensionArray Extending pandas with custom dtypes or arrays. Typing type annotations, mypy/pyright type checking labels May 2, 2021
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not deviate from typing we have elsewhere in the codebase. if you think that is wrong then pls update as well (e.g. Series et all).

@@ -385,7 +387,7 @@ def __iter__(self):
for i in range(len(self)):
yield self[i]

def __contains__(self, item) -> bool | np.bool_:
def __contains__(self, item: Any) -> bool | np.bool_:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we type this with Label elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I did a grep and only saw Any

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Label is for index values, this is an array, so Any is correct I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Label was an alias for Union[Hashable, None] to workaround a mypy bug that has since been fixed. Now mypy recognizes None as being Hashable.

wrt Any

from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#incomplete-stubs

Included functions and methods must list all arguments, but the arguments can be left unannotated. Do not use Any to mark unannotated arguments or return values.

from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions

When adding type hints, avoid using the Any type when possible. Reserve the use of Any for when:

the correct type cannot be expressed in the current type system; and
to avoid Union returns (see above).

Note that Any is not the correct type to use if you want to indicate that some function can accept literally anything: in those cases use object instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on that explanation, it should be object here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used object, but had to do a type ignore when any() was called on a comparison. I couldn't figure out how to work around that. See latest commit.

@Dr-Irv Dr-Irv requested a review from jreback May 3, 2021 10:40
@jreback jreback added this to the 1.3 milestone May 5, 2021
@Dr-Irv Dr-Irv requested a review from jreback May 5, 2021 20:44
@jreback jreback merged commit 19b0e7a into pandas-dev:master May 6, 2021
@jreback
Copy link
Contributor

jreback commented May 6, 2021

thanks @Dr-Irv very nice

@Dr-Irv Dr-Irv deleted the iter_contains branch May 6, 2021 02:19
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants